package com.dean.pokeking.ui.pages

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.Settings
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.lifecycle.viewmodel.viewModelFactory
import com.dean.pokeking.PLACE_FENGYUAN
import com.dean.pokeking.PLACE_LIST
import com.dean.pokeking.PageType
import com.dean.pokeking.R
import com.dean.pokeking.beans.NpcButton
import com.dean.pokeking.beans.PlaceButton
import com.dean.pokeking.ui.MainViewModel
import com.dean.pokeking.ui.theme.PokekingComposeTheme
import com.dean.pokeking.utils.FakeDataUtil

/**
 * @ClassName MainPage
 * @Description MainPage
 * @Author Dean
 * @Date 2023/3/2
 * @Version 1.0
 */

@Composable
fun MainPage(
    currentTeam: String,
    reCopyExcels:() -> Unit
) {
    PokekingComposeTheme {
        val viewModel = viewModel<MainViewModel>()
        viewModel.initData()
        MainNavigationPage(
            pageType = viewModel.mainState.collectAsState().value.currentPage,
            reCopyExcels = reCopyExcels
        )
    }
}

@Composable
fun MainNavigationPage(
    pageType: PageType,
    reCopyExcels:() -> Unit
) {
    val viewModel: MainViewModel = viewModel()
    val mainState = viewModel.mainState.collectAsState().value
    val currentPage = mainState.currentPage
    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(MaterialTheme.colors.primary)
    ) {
        var showMenu by remember { mutableStateOf(false) }
        Scaffold(
            modifier = Modifier
                .fillMaxSize()
                .systemBarsPadding(),
            backgroundColor = MaterialTheme.colors.background,
            topBar = {
                TopAppBar {
                    DropdownMenu(expanded = showMenu, onDismissRequest = { showMenu = false }) {
                        DropdownMenuItem(onClick = {
                            viewModel.setCurrentPage(PageType.PlaceChoosePage)
                            showMenu = false
                        }) {
                            Text(modifier = Modifier.padding(vertical = 5.dp), text = "重选地区")
                        }
                        DropdownMenuItem(onClick = {
                            viewModel.setCurrentPage(PageType.EnemyChoosePage)
                            showMenu = false
                        }) {
                            Text(modifier = Modifier.padding(vertical = 5.dp), text = "重选天王")
                        }
                    }
                    if(mainState.currentPage == PageType.SettingsPage) {
                        IconButton(onClick = {
                            viewModel.setCurrentPage(PageType.PlaceChoosePage)
                        }) {
                            Icon(imageVector = ImageVector.vectorResource(id = R.drawable.ic_save), contentDescription = "")
                        }
                    } else {
                        IconButton(onClick = { showMenu = !showMenu }) {
                            Icon(imageVector = Icons.Filled.Menu, contentDescription = "")
                        }
                    }
                    var text = "选择地区"
                    if (mainState.currentPage == PageType.EnemyChoosePage) text = mainState.currentPlace
                    if (mainState.currentPage == PageType.NpcTeamInfoPage
                        || mainState.currentPage == PageType.MonstersChoosePage)
                        text = "${mainState.currentPlace}/${mainState.currentNpc}"
                    Text(
                        modifier = Modifier.weight(1f),
                        text = text,
                        fontSize = 25.sp,
                        textAlign = TextAlign.Center
                    )
                    if (currentPage == PageType.NpcTeamInfoPage) {
                        IconButton(
                            onClick = {
                                viewModel.showFilterPage(!mainState.showFilterPage)
                            }
                        ) {
                            Icon(imageVector = ImageVector.vectorResource(id = R.drawable.ic_filter), contentDescription = "")
                        }
                    }else if (currentPage == PageType.PlaceChoosePage) {
                        IconButton(onClick = {
                            //打开设置页面
                            viewModel.gotoSettingsPage()
                        }) {
                            Icon(imageVector = Icons.Filled.Settings, contentDescription = "")
                        }
                    } else {
                        Spacer(modifier = Modifier.width(48.dp))
                    }
                }
            },
            floatingActionButton = {
                if (currentPage != PageType.PlaceChoosePage && currentPage != PageType.EnemyChoosePage && currentPage != PageType.SettingsPage ) {
                    Button(onClick = { viewModel.gotoNextNpc() }) {
                        Text(text = "下一个天王")
                    }
                }
            },
            isFloatingActionButtonDocked = false
        ) { paddingValues ->
            when (pageType) {
                PageType.PlaceChoosePage -> PlaceChoosePage(
                    modifier = Modifier.padding(paddingValues),
                    placeButtonList = PLACE_LIST,
                    onClick = { viewModel.setCurrentPlace(it) }
                )
                PageType.EnemyChoosePage -> EnemyChoosePage(
                    modifier = Modifier.padding(paddingValues),
                    onClick = { viewModel.setCurrentNpc(it) }
                )
                PageType.MonstersChoosePage -> MonstersChoosePage(
                    modifier = Modifier.padding(paddingValues),
                    onClick = { viewModel.setCurrentMonster(it) }
                )
                PageType.NpcTeamInfoPage -> NpcTeamInfoPage()
                PageType.SettingsPage -> SettingsPage(
                    teamList = mainState.teamList,
                    selectTeamName = mainState.currentTeam,
                    onTeamSelect = { viewModel.setCurrentTeam(it) },
                    reCopyExcels = reCopyExcels
                )
            }
        }
    }
}


@Preview(showBackground = true)
@Composable
fun HomePagePreview() {
    MainPage("小蛋队"){}
}

